package vn.sms.lazylove;

import java.util.ArrayList;
import java.util.List;

import vn.sms.lazylove.SMSPatentActivity.SMSPatentLoadTask;
import vn.sms.lazylove.adapter.SendSmsAdapter;
import vn.sms.lazylove.adapter.SmsAdapter;
import vn.sms.lazylove.database.DataStore;
import vn.sms.lazylove.gui.DialogHandler;
import vn.sms.lazylove.object.SendSms;
import vn.sms.lazylove.object.Sms;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SendSMSActivity extends Activity {

	private SendSmsAdapter mListSendSMSAdapter = null;
	private ProgressDialog mProgressBar = null;
	private ListView mListView;
	private Activity mActivity = null;
	private Context mContext;
	DataStore mDatastore;
	Button btnBack, btnEdit;
	private boolean mIsEditing = false;

	private static final int DELETE_SUCCESS = 1;
	private static final int DELETE_FAIL = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_sendsms);
		mActivity = this;
		mContext = this;
		DataStore.init(mContext);
		mDatastore = DataStore.getInstance();
		mListView = (ListView) findViewById(R.id.listview_sendsms);
		TextView txtTitle = (TextView) findViewById(R.id.txt_title);
		txtTitle.setText(getString(R.string.txt_send_sms));
		btnEdit = (Button) findViewById(R.id.btn_right);
		btnBack = (Button) findViewById(R.id.btn_left);

		btnEdit.setOnClickListener(onEditBtnClickListener);
		btnBack.setOnClickListener(onBackBtnClickListener);
	}

	OnClickListener onBackBtnClickListener = new OnClickListener() {

		public void onClick(View v) {
			onBackPressed();
		}
	};

	OnClickListener onEditBtnClickListener = new OnClickListener() {

		public void onClick(View v) {
			if (mListSendSMSAdapter != null) {
				if (mIsEditing) {
					mIsEditing = false;
					btnEdit.setBackgroundResource(R.drawable.selector_button_edit);
					mListSendSMSAdapter.notifyListObjectChanged(false);
					// request to server here
					if (mListSendSMSAdapter.getListDeleted().size() > 0){
						//deleteSendSMS();
						doclick();
					}
						
				} else {
					mIsEditing = true;
					btnEdit.setBackgroundResource(R.drawable.selector_button_done);
					mListSendSMSAdapter.notifyListObjectChanged(true);
				}
			}

		}
	};

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			hideLoadingControl();
			switch (msg.what) {
			case DELETE_FAIL:
				Toast.makeText(mContext, getString(R.string.delete_fail),
						Toast.LENGTH_SHORT).show();
				break;
			case DELETE_SUCCESS:
				Toast.makeText(mContext, getString(R.string.delete_success),
						Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};

	private void deleteSendSMS() {
		showLoadingControl();
		Thread dataInitializationThread = new Thread() {
			public void run() {
				int count = 0;
				for (Object obj : mListSendSMSAdapter.getListDeleted()) {
					if (obj instanceof SendSms) {
						SendSms sms = (SendSms) obj;
						if (mDatastore.removeListSendSMS(sms))
							count++;
					}
				}
				if (count == mListSendSMSAdapter.getListDeleted().size())
					mHandler.sendEmptyMessage(DELETE_SUCCESS);
				else
					mHandler.sendEmptyMessage(DELETE_FAIL);
			}
		};
		dataInitializationThread.start();
	}

	private List<SendSms> getListSendSMS() {
		List<SendSms> sendsms = new ArrayList<SendSms>();
		sendsms = mDatastore.getListSendSMS();

		return sendsms;
	}

	private void showLoadingControl() {
		mProgressBar = ProgressDialog.show(mActivity, "",
				getString(R.string.loading_default), true);

	}

	private void hideLoadingControl() {
		if (mProgressBar != null && mProgressBar.isShowing())
			mProgressBar.dismiss();

	}

	class SendSMSLoadTask extends AsyncTask<Integer, Integer, List<SendSms>> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (mListSendSMSAdapter == null) {
				showLoadingControl();
			}
		}

		@Override
		protected List<SendSms> doInBackground(Integer... params) {
			List<SendSms> objs = new ArrayList<SendSms>();
			try {
				objs = getListSendSMS();
				return objs;
			} catch (Exception ex) {
				return objs;
			}
		}

		@Override
		protected void onPostExecute(List<SendSms> result) {
			super.onPostExecute(result);
			hideLoadingControl();
			mListSendSMSAdapter = new SendSmsAdapter(mActivity, result);
			mListView.setAdapter(mListSendSMSAdapter);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_sendsms, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {		
		default:
			finish();
			break;
		}
		return true;
	}

	@Override
	protected void onResume() {
		btnEdit.setBackgroundResource(R.drawable.selector_button_edit);
		new SendSMSLoadTask().execute(0);
		super.onResume();		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	public void doclick() {
		DialogHandler appdialog = new DialogHandler();
		boolean dlg = appdialog.Confirm(this,
				getString(R.string.txt_confirmation),
				getString(R.string.txt_are_you_sure_you_want_to_delete),
				"Cancel", "OK", aproc(), bproc());
	}

	public Runnable aproc() {
		return new Runnable() {
			public void run() {
				Log.d("Delete sms", "OK");
				deleteSendSMS();
			}
		};
	}

	public Runnable bproc() {
		return new Runnable() {
			public void run() {
				Log.d("Delete sms", "Cancel");
				btnEdit.setBackgroundResource(R.drawable.selector_button_edit);
				new SendSMSLoadTask().execute(0);
			}
		};
	}

}
